home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / netlib / lib / rinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-27  |  6.5 KB  |  202 lines

  1. /*
  2.  * Adopted to Mint-Net 1994, Kay Roemer
  3.  */
  4.  
  5. /*-
  6.  * Copyright (c) 1985, 1989 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)res_init.c    6.15 (Berkeley) 2/24/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. #include "socklib.h"
  43. #include <sys/param.h>
  44. #include <sys/socket.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <arpa/nameser.h>
  48. #include <resolv.h>
  49. #include <unistd.h>
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <support.h>
  54.  
  55. /*
  56.  * Resolver state default settings
  57.  */
  58.  
  59. struct state _res = {
  60.     RES_TIMEOUT,                   /* retransmition time interval */
  61.     4,                             /* number of times to retransmit */
  62.     RES_DEFAULT,            /* options flags */
  63.     1,                             /* number of name servers */
  64. };
  65.  
  66. /*
  67.  * Set up default settings.  If the configuration file exist, the values
  68.  * there will have precedence.  Otherwise, the server address is set to
  69.  * INADDR_ANY and the default domain name comes from the gethostname().
  70.  *
  71.  * As of 4.4 BSD the default name server address is 127.0.0.1. So we do.
  72.  *
  73.  * The configuration file should only be used if you want to redefine your
  74.  * domain or run without a server on your machine.
  75.  *
  76.  * Return 0 if completes successfully, -1 on error
  77.  */
  78. int
  79. res_init()
  80. {
  81.     register FILE *fp;
  82.     register char *cp, **pp;
  83.     register int n;
  84.     char buf[BUFSIZ];
  85.     int nserv = 0;    /* number of nameserver records read from file */
  86.     int haveenv = 0;
  87.     int havesearch = 0;
  88.  
  89.     _res.nsaddr.sin_addr = inet_makeaddr (IN_LOOPBACKNET, 1);
  90.     _res.nsaddr.sin_family = AF_INET;
  91.     _res.nsaddr.sin_port = htons(NAMESERVER_PORT);
  92.     _res.nscount = 1;
  93.  
  94.     /* Allow user to override the local domain definition */
  95.     if ((cp = getenv("LOCALDOMAIN")) != NULL) {
  96.         (void)strncpy(_res.defdname, cp, sizeof(_res.defdname));
  97.         haveenv++;
  98.     }
  99.  
  100.     if ((fp = fopen(_PATH_RESCONF, "rt")) != NULL) {
  101.         /* read the config file */
  102.         while (fgets(buf, sizeof(buf), fp) != NULL) {
  103.         /* read default domain name */
  104.         if (!strncmp(buf, "domain", sizeof("domain") - 1)) {
  105.             if (haveenv)    /* skip if have from environ */
  106.                 continue;
  107.             cp = buf + sizeof("domain") - 1;
  108.             while (*cp == ' ' || *cp == '\t')
  109.                 cp++;
  110.             if ((*cp == '\0') || (*cp == '\n'))
  111.                 continue;
  112.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  113.             if ((cp = index(_res.defdname, '\n')) != NULL)
  114.                 *cp = '\0';
  115.             havesearch = 0;
  116.             continue;
  117.         }
  118.         /* set search list */
  119.         if (!strncmp(buf, "search", sizeof("search") - 1)) {
  120.             if (haveenv)    /* skip if have from environ */
  121.                 continue;
  122.             cp = buf + sizeof("search") - 1;
  123.             while (*cp == ' ' || *cp == '\t')
  124.                 cp++;
  125.             if ((*cp == '\0') || (*cp == '\n'))
  126.                 continue;
  127.             (void)strncpy(_res.defdname, cp, sizeof(_res.defdname) - 1);
  128.             if ((cp = index(_res.defdname, '\n')) != NULL)
  129.                 *cp = '\0';
  130.             /*
  131.              * Set search list to be blank-separated strings
  132.              * on rest of line.
  133.              */
  134.             cp = _res.defdname;
  135.             pp = _res.dnsrch;
  136.             *pp++ = cp;
  137.             for (n = 0; *cp && pp < _res.dnsrch + MAXDNSRCH; cp++) {
  138.                 if (*cp == ' ' || *cp == '\t') {
  139.                     *cp = 0;
  140.                     n = 1;
  141.                 } else if (n) {
  142.                     *pp++ = cp;
  143.                     n = 0;
  144.                 }
  145.             }
  146.             /* null terminate last domain if there are excess */
  147.             while (*cp != '\0' && *cp != ' ' && *cp != '\t')
  148.                 cp++;
  149.             *cp = '\0';
  150.             *pp++ = 0;
  151.             havesearch = 1;
  152.             continue;
  153.         }
  154.         /* read nameservers to query */
  155.         if (!strncmp(buf, "nameserver", sizeof("nameserver") - 1) &&
  156.            nserv < MAXNS) {
  157.             cp = buf + sizeof("nameserver") - 1;
  158.             while (*cp == ' ' || *cp == '\t')
  159.                 cp++;
  160.             if ((*cp == '\0') || (*cp == '\n'))
  161.                 continue;
  162.             if ((_res.nsaddr_list[nserv].sin_addr.s_addr =
  163.             inet_addr(cp)) == (unsigned)-1) {
  164.                 _res.nsaddr_list[nserv].sin_addr.s_addr
  165.                 = INADDR_ANY;
  166.                 continue;
  167.             }
  168.             _res.nsaddr_list[nserv].sin_family = AF_INET;
  169.             _res.nsaddr_list[nserv].sin_port = htons(NAMESERVER_PORT);
  170.             nserv++;
  171.             continue;
  172.         }
  173.         }
  174.         if (nserv > 1) 
  175.         _res.nscount = nserv;
  176.         (void) fclose(fp);
  177.     }
  178.     if (_res.defdname[0] == 0) {
  179.         if (gethostname(buf, sizeof(_res.defdname)) == 0 &&
  180.            (cp = index(buf, '.')))
  181.             (void)strcpy(_res.defdname, cp + 1);
  182.     }
  183.  
  184.     /* find components of local domain that might be searched */
  185.     if (havesearch == 0) {
  186.         pp = _res.dnsrch;
  187.         *pp++ = _res.defdname;
  188.         for (cp = _res.defdname, n = 0; *cp; cp++)
  189.             if (*cp == '.')
  190.                 n++;
  191.         cp = _res.defdname;
  192.         for (; n >= LOCALDOMAINPARTS && pp < _res.dnsrch + MAXDFLSRCH;
  193.             n--) {
  194.             cp = index(cp, '.');
  195.             *pp++ = ++cp;
  196.         }
  197.         *pp++ = 0;
  198.     }
  199.     _res.options |= RES_INIT;
  200.     return (0);
  201. }
  202.